package com.ruoyi.web.controller.market.sys;

import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.AddressInfo;
import com.ruoyi.sys.service.IAddressInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.AddressInfoWapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 地址管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Controller
@RequestMapping("/system/address")
public class AddressInfoController extends BaseController
{
    private String prefix = "sys/address";

    @Autowired
    private IAddressInfoService addressInfoService;

    @RequiresPermissions("system:address:view")
    @GetMapping()
    public String address()
    {
        return prefix + "/address";
    }

    /**
     * 查询地址管理树列表
     */
    @RequiresPermissions("system:address:list")
    @PostMapping("/list")
    @ResponseBody
    public List<AddressInfo> list(AddressInfo addressInfo)
    {
        List<AddressInfo> list = addressInfoService.selectAddressInfoList(addressInfo);
        return list;
    }

    /**
     * 导出地址管理列表
     */
    @RequiresPermissions("system:address:export")
    @Log(title = "地址管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AddressInfo addressInfo)
    {
        List<AddressInfo> list = addressInfoService.selectAddressInfoList(addressInfo);
        ExcelUtil<AddressInfo> util = new ExcelUtil<AddressInfo>(AddressInfo.class);
        return util.exportExcel(list, "地址管理数据");
    }

    /**
     * 新增地址管理
     */
    @GetMapping(value = { "/add/{codeValue}", "/add/" })
    public String add(@PathVariable(value = "codeValue", required = false) Long codeValue, ModelMap mmap)
    {
        if (StringUtils.isNotNull(codeValue))
        {
            mmap.put("addressInfo", addressInfoService.selectAddressInfoByCodeValue(String.valueOf(codeValue)));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存地址管理
     */
    @RequiresPermissions("system:address:add")
    @Log(title = "地址管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AddressInfo addressInfo)
    {
        return toAjax(addressInfoService.insertAddressInfo(addressInfo));
    }

    /**
     * 修改地址管理
     */
    @RequiresPermissions("system:address:edit")
    @GetMapping("/edit/{codeValue}")
    public String edit(@PathVariable("codeValue") String codeValue, ModelMap mmap)
    {
        AddressInfo addressInfo = addressInfoService.selectAddressInfoByCodeValue(codeValue);
        mmap.put("addressInfo", addressInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存地址管理
     */
    @RequiresPermissions("system:address:edit")
    @Log(title = "地址管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AddressInfo addressInfo)
    {
        return toAjax(addressInfoService.updateAddressInfo(addressInfo));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:address:remove")
    @Log(title = "地址管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{codeValue}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("codeValue") String codeValue)
    {
        return toAjax(addressInfoService.deleteAddressInfoByCodeValue(codeValue));
    }

    /**
     * 选择地址管理树
     */
    @GetMapping(value = { "/selectAddressTree/{codeValue}", "/selectAddressTree/" })
    public String selectAddressTree(@PathVariable(value = "codeValue", required = false) Long codeValue, ModelMap mmap)
    {
        if (StringUtils.isNotNull(codeValue))
        {
            mmap.put("addressInfo", addressInfoService.selectAddressInfoByCodeValue(String.valueOf(codeValue)));
        }
        return prefix + "/tree";
    }

    /**
     * 加载地址管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = addressInfoService.selectAddressInfoTree();
        return ztrees;
    }

    @GetMapping(value = { "/muliChose"})
    public String muliChose()
    {
        return prefix + "/muliChose";
    }
}
